/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.sqlServer.PPOutboundMission;
import me.zhengjie.repository.PPOutboundMissionRepository;
import me.zhengjie.service.PPOutboundMissionService;
import me.zhengjie.service.dto.PPOutboundMissionDto;
import me.zhengjie.service.dto.PPOutboundMissionQueryCriteria;
import me.zhengjie.service.mapper.PPOutboundMissionMapper;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-14
**/
@Service
@RequiredArgsConstructor
public class PPOutboundMissionServiceImpl implements PPOutboundMissionService {

    private final PPOutboundMissionRepository ppOutboundMissionRepository;
    private final PPOutboundMissionMapper ppOutboundMissionMapper;

    @Override
    public PageResult<PPOutboundMissionDto> queryAll(PPOutboundMissionQueryCriteria criteria, Pageable pageable){
        Page<PPOutboundMission> page = ppOutboundMissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppOutboundMissionMapper::toDto));
    }

    @Override
    public List<PPOutboundMissionDto> queryAll(PPOutboundMissionQueryCriteria criteria){
        return ppOutboundMissionMapper.toDto(ppOutboundMissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PPOutboundMissionDto findById(Integer id) {
        PPOutboundMission ppOutboundMission = ppOutboundMissionRepository.findById(id).orElseGet(PPOutboundMission::new);
        ValidationUtil.isNull(ppOutboundMission.getId(),"PpOutboundMission","id",id);
        return ppOutboundMissionMapper.toDto(ppOutboundMission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PPOutboundMission resources) {
        ppOutboundMissionRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PPOutboundMission resources) {
        PPOutboundMission ppOutboundMission = ppOutboundMissionRepository.findById(resources.getId()).orElseGet(PPOutboundMission::new);
        ValidationUtil.isNull( ppOutboundMission.getId(),"PpOutboundMission","id",resources.getId());
        ppOutboundMission.copy(resources);
        ppOutboundMissionRepository.save(ppOutboundMission);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppOutboundMissionRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PPOutboundMissionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PPOutboundMissionDto ppOutboundMission : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("批次号", ppOutboundMission.getBatchNum());
//            map.put("作业设备型号", ppOutboundMission.getPpDevice());
//            map.put("定位方式", ppOutboundMission.getMethod());
//            map.put("生产时间", ppOutboundMission.getPpPd());
//            map.put("过期时间", ppOutboundMission.getPpExp());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<PPOutboundMission> findPPOutBoundMissionByOrderId(Integer orderId) {
       return ppOutboundMissionRepository.findPPOutboundMissionsByOutBoundOrderId(orderId);
    }

    @Override
    public void startMission(List<PPOutboundMission> ppOutBoundMissions) {
        for (PPOutboundMission ppOutboundMission : ppOutBoundMissions) {
            ppOutboundMission.setMissionStatus(1);
            ppOutboundMissionRepository.save(ppOutboundMission);
        }
    }

}